package vip.specimen.system.service;

import com.alibaba.fastjson.JSONObject;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import vip.specimen.system.datasource.entity.SystemArticleCategoriesEntity;
import vip.specimen.system.repository.SystemArticleCategoriesRepository;
import vip.specimen.system.datasource.validation.SystemCategoriesValidation;
import vip.specimen.system.utils.CommonUtil;
import vip.specimen.system.utils.DataBaseUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * *@Author: 余浪人
 * *@Date：2021/4/7 14:56
 * *@Email:yulangren520@Gmail.com
 * *@QQ:1368419640
 **/
@Service
public class SystemArticleCategoriesService {
    @Resource
    private SystemArticleCategoriesRepository articleCategoriesRepository;
    @Resource
    private HttpServletRequest request;

    /**
     * 新增分类
     *
     * @param categoriesValidation 验证体
     * @return 实体
     */
    @Transactional
    public SystemArticleCategoriesEntity insert(SystemCategoriesValidation categoriesValidation) {
        SystemArticleCategoriesEntity entity = null != categoriesValidation.getSuperior() ? this.articleCategoriesRepository.findById(categoriesValidation.getSuperior()).get() : null;
        return this.articleCategoriesRepository.save(new SystemArticleCategoriesEntity(categoriesValidation.getCategoriesName(), categoriesValidation.getSequence(), categoriesValidation.getCategoriesKeyword(), categoriesValidation.getCategoriesDescription(), entity));
    }

    /**
     * 根据分类名称查询
     *
     * @param name 分类名称
     * @return 分类实体
     */
    public SystemArticleCategoriesEntity findByCategoriesName(String name) {
        return this.articleCategoriesRepository.findByCategoriesName(name);
    }

    /**
     * 查询
     *
     * @return 实体集
     */
    public Page<SystemArticleCategoriesEntity> findAllPage() {
        JSONObject param = CommonUtil.pageParams(this.request);
        return this.articleCategoriesRepository.findAll(DataBaseUtil.getPageable(param.getIntValue("page"), param.getIntValue("pageSize"), DataBaseUtil.getSort("desc", "createTime")));
    }

    /**
     * 获取全部 => 创建时间倒序
     *
     * @return res
     */
    public List<SystemArticleCategoriesEntity> findAll() {
        return this.articleCategoriesRepository.findAll(DataBaseUtil.getSort("desc", "createTime"));
    }

    /**
     * 获取全部 => 排序字段 倒序
     *
     * @return res
     */
    public List<SystemArticleCategoriesEntity> findAllSequence() {
        return this.articleCategoriesRepository.findAll(DataBaseUtil.getSort("desc", "sequence"));
    }

    public List<SystemArticleCategoriesEntity> findAllById(Set<UUID> uuidList) {
        return this.articleCategoriesRepository.findAllById(uuidList);
    }

    /**
     * 根据状态
     *
     * @param uuid uuid
     * @return 实体
     */
    @Transactional
    public SystemArticleCategoriesEntity update(UUID uuid) {
        if (this.articleCategoriesRepository.existsById(uuid)) {
            SystemArticleCategoriesEntity articleCategoriesEntity = this.articleCategoriesRepository.findById(uuid).get();
            articleCategoriesEntity.setEnable(!articleCategoriesEntity.isEnable());
            articleCategoriesEntity.setUpdateTime(Calendar.getInstance().getTime());
            return this.articleCategoriesRepository.save(articleCategoriesEntity);
        }
        return null;
    }

    /**
     * 更新排序
     *
     * @param uuid     uuid
     * @param sequence 序号
     * @return res
     */
    @Transactional
    public SystemArticleCategoriesEntity update(UUID uuid, Integer sequence) {
        if (this.articleCategoriesRepository.existsById(uuid)) {
            SystemArticleCategoriesEntity articleCategoriesEntity = this.articleCategoriesRepository.findById(uuid).get();
            articleCategoriesEntity.setSequence(sequence);
            articleCategoriesEntity.setUpdateTime(Calendar.getInstance().getTime());
            return this.articleCategoriesRepository.save(articleCategoriesEntity);
        }
        return null;
    }

    /**
     * 更新
     *
     * @param uuid                 uuid
     * @param categoriesValidation 验证体
     * @return res
     */
    @Transactional
    public SystemArticleCategoriesEntity update(UUID uuid, SystemCategoriesValidation categoriesValidation) {
        if (this.articleCategoriesRepository.existsById(uuid)) {
            SystemArticleCategoriesEntity articleCategoriesEntity = this.articleCategoriesRepository.findById(uuid).get();
            articleCategoriesEntity.setCategoriesDescription(categoriesValidation.getCategoriesDescription());
            articleCategoriesEntity.setCategoriesKeyword(categoriesValidation.getCategoriesKeyword());
            articleCategoriesEntity.setCategoriesName(categoriesValidation.getCategoriesName());
            articleCategoriesEntity.setSequence(categoriesValidation.getSequence());
            SystemArticleCategoriesEntity entity = null != categoriesValidation.getSuperior() ? this.articleCategoriesRepository.findById(categoriesValidation.getSuperior()).get() : null;
            articleCategoriesEntity.setSuperior(entity);
            articleCategoriesEntity.setUpdateTime(Calendar.getInstance().getTime());
            return this.articleCategoriesRepository.save(articleCategoriesEntity);
        }
        return null;
    }

    /**
     * 删除
     *
     * @param uuid uuid
     * @return res
     */
    @Transactional
    public boolean delete(UUID uuid) {
        if (!this.articleCategoriesRepository.existsById(uuid)) return false;
        this.articleCategoriesRepository.deleteById(uuid);
        return true;
    }


    @Transactional
    public int delete(Set<UUID> uuids) {
        int successNum = 0;
        for (UUID uuid : uuids) {
            successNum += this.delete(uuid) ? 1 : 0;
        }
        return successNum;
    }

    public SystemArticleCategoriesEntity findById(UUID uuid) {
        return this.articleCategoriesRepository.findById(uuid).get();
    }
}
